package com.thirdnf.calendar;

import org.jetbrains.annotations.NotNull;

import java.util.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;


public class DirectionButton extends JPanel implements MouseListener {

    public enum Direction {Left, Right}

    private static int height = 20;
    private static int width = 25;

    private final List<ActionListener> _actionListeners;

    private final Border OverBorder = BorderFactory.createLineBorder(Color.black);
    private final Border OutBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1);

    private Shape _shape;

    public DirectionButton() {

        setSize(width, height);
        setMinimumSize(new Dimension(width, height));
        setPreferredSize(new Dimension(width, height));

        _actionListeners = new ArrayList<ActionListener>();
        setBackground(Color.lightGray);
        setOpaque(false);

        setBorder(OutBorder);

        addMouseListener(this);

        _shape = drawShape(Direction.Right);
    }


    private static Shape drawShape(@NotNull Direction direction)
    {
        int xCenter = width/2;
        int yCenter = height/2;

        int xForth = width/4;
        int yForth = height/4;

        int[] xPoints;
        int[] yPoints = new int[] {yCenter-yForth, yCenter+yForth, yCenter};

        if (direction == Direction.Right) {
            xPoints = new int[] {xCenter-xForth, xCenter-xForth, xCenter+xForth};
        }
        else {
            xPoints = new int[] {xCenter+xForth, xCenter+xForth, xCenter-xForth};
        }

        return new Polygon(xPoints, yPoints, 3);
    }


    public void setDirection(@NotNull Direction direction) {

        _shape = drawShape(direction);
    }


    public void addActionListener(ActionListener l) {
        _actionListeners.add(l);
    }


    @Override
    public void mouseClicked(MouseEvent e) {

        for (ActionListener listener : _actionListeners) {
            listener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "click"));
        }
    }


    @Override
    public void paint(Graphics g)
    {
        super.paint(g);

        Graphics2D graphics = (Graphics2D)g;

        Color oldColor = graphics.getColor();

        graphics.setColor(Color.black);

        graphics.fill(_shape);

        graphics.setColor(oldColor);
    }


    @Override
    public void mousePressed(MouseEvent e) {
        setBackground(Color.darkGray);

//        setOpaque(true);
        setBorder(OverBorder);
    }


    @Override
    public void mouseReleased(MouseEvent e) {
        setBackground(Color.lightGray);
//        setOpaque(false);
        setBorder(OutBorder);
    }


    @Override
    public void mouseEntered(MouseEvent e) {

//        setOpaque(true);
        setBorder(OverBorder);
    }


    @Override
    public void mouseExited(MouseEvent e) {
        // Always do this, just in case we set it to disabled before we existed.
//        if (! _enabled) { return; }

        // Complete reset
//        setOpaque(false);
        setBorder(OutBorder);
    }


}
